<template>
	<view class="apps">
		<view class="flex fdc plr20" v-if="id > 0">
			<view class="flex plr10 ptb10 mb20 pr" :style="id ==2?style[0]:style[1]">
				<view class="f22 white" style="position:absolute;bottom:15rpx;right:15rpx;">最终解释权归地平线便民服务平台所有</view>
				<view class="f1 flex fdc ptb10 plr10">
					<view class="flex aic">
						<uv-avatar :src="logo"></uv-avatar>
						<view class="f26 white ml20">{{product.title}}</view>
					</view>
					
					<view class="flex mtb20 jcsa">
						
						<view class="white flex aic f22 ptb10" v-if="product.is_vip == 1">300元洗车年费</view>
						<view class="white flex aic f22 ptb10" v-if="product.is_vip == 0">300元洗车充值卡</view>
					</view>
					<view class="flex white aic">
						<view class="f18">￥</view><view class="ft30">{{product.price}}</view><view class="ml10 f26" v-if="product.gift">赠送 ￥{{item.gift}} 优惠券</view>
					</view>
				</view>
				<view class="pro_right"><image :src="image" ></image></view>
			</view>
			<view class="flex fdc plr10 ptb10 mb20 bgwithe" v-if="id == 2">
				<view class="plr10 ptb10 f14 red">购买须知：</view>
				<view class="flex aic plr10 ptb10 mtb10" style="background-color:#FF9800;border-radius:5px;">
					<view class="f26 f1 white">1.此卡为年卡，有效期为1年，每年洗车费300元，最低消费每次6元,到期洗车费自动清零</view>
					<uv-avatar src="https://mp.lifecloud.top/static/images/cash.png" size="50"></uv-avatar>
				</view>
				<view class="flex aic plr10 ptb10 mtb10" style="background-color:#2196f3;border-radius:5px;">
					<uv-avatar src="https://mp.lifecloud.top/static/images/dc.png" size="50"></uv-avatar>
					<view class="f26 f1 white">2.赠送电瓶抵扣券100元，在平台购买电瓶可以抵扣100元</view>
				</view>
				<view class="flex aic plr10 ptb10 mtb10" style="background-color:#8bc34a;border-radius:5px;">
					<view class="f26 f1 white">3.赠送水果积分券200积分，平台不定期举行水果购优惠活动，可用积分换购水果赠送亲友(西藏除外)，1积分相当于1元</view>
					<uv-avatar src="https://mp.lifecloud.top/static/images/sg.png" size="50"></uv-avatar>
				</view>
				<view class="plr10 ptb10 f26 white" :style="style[0]">特别提醒:点击我要购买视为同意以上条款，由于赠送价值高昂，购买后不能退款</view>
			</view>
			<view class="flex fdc plr10 ptb10 mb20 bgwithe" v-if="id == 1">
				<view class="plr10 ptb10 f14 red">购买须知：</view>
				<view class="flex aic plr10 ptb10 mtb10" style="background-color:#FF9800;border-radius:5px;">
					<view class="f26 f1 white">1.此卡为充值卡 充值洗车费300元，最低消费每次6元,无其他赠送，每年需交押金30元，购买自动扣费</view>
					<uv-avatar src="https://mp.lifecloud.top/static/images/cash.png" size="50"></uv-avatar>
				</view>
				<view class="plr10 ptb10 f26 white" :style="style[0]">特别提醒:点击我要购买视为同意以上条款，由于赠送价值高昂，购买后不能退款</view>
			</view>
			<view class="ptb20"> <uv-button size="large" type="primary" text="我要购买" @click="openModel"></uv-button></view>
		</view>
		<uv-modal ref="modal" title="购买确认" content='我已阅读并确认同意购买须知条款' confirmText="确认购买" :showCancelButton="true" @confirm="confirm" confirmColor="#af0203"></uv-modal>
	</view>
</template>

<script>
	import { util } from '/common/util.js';
	let app;
	export default {
		data() {
			return {
				is_submit:false,
				product:{},
				id:0,
				logo:"https://mp.lifecloud.top/static/images/logo.jpg",
				image:"https://mp.lifecloud.top/static/images/logo.png",
				style:[
					"background-color:#af0203;border-radius: 4px;",
					"background-color:#7bbf31;border-radius: 4px;",
					
					
				]
			}
		},
		onLoad(opt) {
			this.id = opt.id;
			app = this;
			this.getList();
		},

		methods: {
			openModel(){
				if(this.is_submit){
					return;
				}
				this.$refs.modal.open();
			},
			getList(){
				util.request({
					url:"/shop/detail",
					data:{id:app.id},
					method:"POST",
				}).then((res)=>{
					if(res.code ==1){
						app.product = res.data;
					}
				})
			},
			async confirm() {
				this.is_submit = true;
				this.$refs.modal.close();
				if(util.isLogin == false){
					util.alert('请先登录');
					setTimeout(function(){
						uni.navigateTo({
							url:"/pages/login/login"
						})
					},1500)
					return
				}
				
				const order = await util.request({url:'/shop/cart',data:this.product});
				
				if(order.code == 1){
					const payconfig = await util.request({url:'/shop/payment',data:order.data});
					if(payconfig.code == 1){
						wx.requestPayment({
							timeStamp: payconfig.data.timeStamp,
							nonceStr: payconfig.data.nonceStr,
							package: payconfig.data.package,
							signType: payconfig.data.signType,
							paySign: payconfig.data.paySign,
							success (res) {
								app.is_submit =false;
								util.request({url:'/shop/prepaid',data:order.data});
								util.alert('支付成功');
								setTimeout(function(){
									uni.redirectTo({
										url:"/pages/order/order?id="+payconfig.data.order.id
									})
								},1500)
							},
							fail (err) {
								app.is_submit =false;
								util.request({url:'/shop/payfail',data:order.data});
								if(err.errMsg === 'requestPayment:fail cancel'){
									util.alert('您取消了支付');
								}else{
									util.alert('支付失败');
								}
							},
							comlete(res){
								app.is_submit =false;
								console.log(res);
							}
						})
					}
				}
			}
		}
	}
</script>

<style scoped>
.pro_right{width:240rpx;height:240rpx;}
.pro_right image{width:100%;height:100%}
</style>
